<template>
<div class="home">
  <!--头部搜索框-->
  <!--通过headerScroll变量为true或false，动态添加或删除active类-->
  <header class="home-header wrap" :class="{'active': headerScroll}">
    <!--使用vur-router提供的全局组件router-link进行路由跳转，他的特点是不需要写方法，直接使用组件内的to属性进行页面跳转-->
    <router-link to="./category">
      <!--分类图标，在index.html中，笔者已经添加了项目需要使用的icon字体图标在线链接-->
      <i class="nbicon nbmenu2"/>
    </router-link>
    <!--搜索框内容-->
    <div class="header-search">
      <span class="app-name">新蜂商城</span>
      <i class="iconfont icon-search"></i>
      <!--单击后跳转到商品搜索列表（本章暂不做跳转操作）-->
      <router-link class="search-title" to="./product-list">山河无恙，人间皆安</router-link>
    </div>
    <!--跳转到个人中心页面，如果isLogin为false，则显示登录文字，单击跳转至登录页面，进行鉴权操作-->
    <router-link class="login" to="./login" v-if="!isLogin">登录</router-link>
    <!--如果isLogin为true,表示已经登录，则显示用户头像图标，单击跳转至个人中心页面-->
    <router-link class="login" to="./user" v-else>
      <van-icon name="manager-o"/>
    </router-link>
  </header>
  <!--轮播图组件-->
  <swiper :list="swiperList"></swiper>
  <!--导航栏模块-->
  <div class="category-list">
    <div v-for="item in categoryList" v-bind:key="item.categoryId" @click="tips">
      <img :src="item.imgUrl">
      <span>{{ item.name }}</span>
    </div>
  </div>
  <div class="good">
    <header class="good-header">新品上线</header>
    <!--在数据未加载前，使用骨架屏占位，提升页面的体验-->
    <van-skeleton title :row="3" :loading="loading">
      <div class="good-box">
        <!--v-for循环newGoodses参数， 引入good-item作为循环项-->
        <template v-for="(item, index) in newGoodses" :key="index">
          <!--$filters.prefix为全局过滤项-->
          <good-item 
            :cover="$filters.prefix(item.goodsCoverImg)"
            :name="item.goodsName"
            :price="item.sellingPrice"
            @goToDetail="goToDetail"
            :id="item.goodsId"
          />
        </template>
      </div>
    </van-skeleton>
    <header class="good-header">热门商品</header>
    <van-skeleton title :row="3" :loading="loading">
      <div class="good-box">
        <template v-for="(item, index) in hots" :key="index">
          <good-item 
            :cover="$filters.prefix(item.goodsCoverImg)"
            :name="item.goodsName"
            :price="item.sellingPrice"
            @goToDetail="goToDetail"
            :id="item.goodsId"
          />
        </template>
      </div>
    </van-skeleton>
    <header class="good-header">推荐商品</header>
    <van-skeleton title :row="3" :loading="loading">
      <div class="good-box">
        <template v-for="(item, index) in recommends" :key="index">
          <good-item 
            :cover="$filters.prefix(item.goodsCoverImg)"
            :name="item.goodsName"
            :price="item.sellingPrice"
            @goToDetail="goToDetail"
            :id="item.goodsId"
          />
        </template>
      </div>
    </van-skeleton>
  </div>
  <!--公用底部导航栏-->
  <nav-bar />
</div>
</template>
<script>
import { reactive, onMounted, toRefs, nextTick } from 'vue'
import NavBar from 'components/NavBar.vue' //引入底部导航栏组件
import Swiper from '@/components/Swiper.vue'
import { getLocal } from 'utils/help' //引入工具库方法
import { getHome } from '@/service/home' //引入接口方法
import GoodItem from '@/components/GoodItem.vue'

export default {
  name: 'Home',
  components: {
    NavBar, //注册底部导航组件
    Swiper, //注册轮播图方法
    GoodItem, //商品组件
  },
  setup(){
    const state = reactive({
      isLogin: false, //声明是否登录变量
      swiperList: [], //轮播图列表变量
      categoryList: [ //导航栏数组
        {
          name: '新蜂超市',
          imgUrl: 'https://s.yezgea02.com/1604041127880/%E8%B6%85%E5%B8%82%402x.png',
          categoryId: 100001
        },{
          name: '新蜂服饰',
          imgUrl: 'https://s.yezgea02.com/1604041127880/%E6%9C%8D%E9%A5%B0%402x.png',
          categoryId: 100003
        },{
          name: '全球购',
          imgUrl: 'https://s.yezgea02.com/1604041127880/%E5%85%A8%E7%90%83%E8%B4%AD%402x.png',
          categoryId: 100002
        },{
          name: '新蜂生鲜',
          imgUrl: 'https://s.yezgea02.com/1604041127880/%E7%94%9F%E9%B2%9C%402x.png',
          categoryId: 100004
        },{
          name: '新蜂到家',
          imgUrl: 'https://s.yezgea02.com/1604041127880/%E5%88%B0%E5%AE%B6%402x.png',
          categoryId: 100005
        },{
          name: '充值缴费',
          imgUrl: 'https://s.yezgea02.com/1604041127880/%E5%85%85%E5%80%BC%402x.png',
          categoryId: 100006
        },{
          name: '9.9元拼',
          imgUrl: 'https://s.yezgea02.com/1604041127880/9.9%402x.png',
          categoryId: 10007
        },{
          name: '领劵',
          imgUrl: 'https://s.yezgea02.com/1604041127880/%E9%A2%86%E5%88%B8%402x.png',
          categoryId: 100008
        },{
          name: '省钱',
          imgUrl: 'https://s.yezgea02.com/1604041127880/%E7%9C%81%E9%92%B1%402x.png',
          categoryId: 100009
        },{
          name: '全部',
          imgUrl: 'https://s.yezgea02.com/1604041127880/%E5%85%A8%E9%83%A8%402x.png',
          categoryId: 100010
        }
      ],
      newGoodses: [], //最新商品列表
      hots: [], //热门商品列表
      recommends:[],//推荐商品
      loading: true, //用于骨架屏的显示或隐藏操作
      headerScroll: false //滚动透明判断
    })
    onMounted(async () => {
      const token = getLocal('token') //通过getLocal获取本地token参数
      if(token){
        //当有token时，赋值isLogin为true
        state.isLogin = true 
      }
      const { data } = await getHome();
      //接口返回字段描述
      /**
       * //轮播图数组
       * carousels: [
       *  carouselUrl: 图片地址
       *  redirectUrl: 跳转地址
       * ]
       */
       state.swiperList = data.carousels // 赋值轮播图列表变量swiperList
        //最新商品数据结构如下
        /**
         * {
         *  newGoodses:[{
         *   goodsCoverImg: 商品图片
         *   goodsId: 商品ID
         *   goodsIntro: 商品介绍 
         *   goodsName: 商品名称
         *   sellingPrice: 商品价格
         *   tag: 标签
         *  }]
         * }
         */
        state.newGoodses = data.newGoodses //最新商品赋值
        state.hots = data.hotGoodses
        state.recommends = data.recommendGoodses
        state.loading = false
    })
    //前往商品详情页面
    const goToDetail = (id) => {
      console.log('前往商品详情页面', id)
    }
    
    nextTick(() => {
      const homeNode = document.querySelector('.home')
      console.log(homeNode)
      homeNode.addEventListener('scroll', () => {
        //获取滚动条距离顶部的高度
        let scrollTop = homeNode.pageYOffset || homeNode.scrollTop || homeNode.scrollTop
        //当高度大于100px时，修改headerScroll的值为true，否则为false
        scrollTop > 100 ? state.headerScroll = true : state.headerScroll = false
      })
    })

    //返回变量给template使用
    return {
      ...toRefs(state),
      goToDetail
    }


  }
}
</script>

<style lang="less" scoped>
@import '../theme/custom';
.home {
  height: 100%;
  overflow: hidden;
  overflow-y: scroll;
}
.home-header {
  position: fixed;
  left: 0;
  top: 0;
  .wh(100%, 50px);
  .fj();
  line-height: 50px;
  padding: 0 15px;
  .boxSizing();
  font-size: 15px;
  color: #fff;
  z-index: 10000;
  .nbmenu2{
    color: @primary;
  }
  &.active {
      background: @primary;
  }
  .header-search{
    display: flex;
    .wh(74%, 30px);
    line-height: 20px;
    margin: 10px 0;
    padding: 5px 0;
    color: #232326;
    background: rgba(255, 255, 255, .7);
    border-radius: 20px;
    .app-name {
      padding: 0 10px;
      color: @primary;
      font-size: 20px;
      font-weight: bold;
      border-right: 1px solid #666;
    }
    .icon-search {
      padding: 0 10px;
      font-size: 17px;
    }
    .search-title{
      font-size: 12px;
      color: #666;
      line-height: 21px;
    }
  }
  .icon-iconyonghu{
    color: #fff;
    font-size: 22px;
  }
  .login{
    color: @primary;
    line-height: 52px;
    .van-icon-manager-o{
      font-size: 20px;
      vertical-align: -3px;
    }
  }
}
.category-list{
  display: flex;
  flex-shrink: 0;
  flex-wrap: wrap;
  width: 100%;
  padding-bottom: 13px;
  div{
    display: flex;
    flex-direction: column;
    width: 20%;
    text-align: center;
    img{
      .wh(36px, 36px);
      margin: 13px auto 8px auto;
    }
  }
}
.good {
  padding-bottom: 50px;
  .good-header {
    background: #f9f9f9;
    height: 50px;
    line-height: 50p;
    text-align: center;
    color: @primary;
    font-size: 16px;
    font-weight: 500;
  }
  .good-box{
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
  }
}
</style>